package com.gxa.controller;

import com.gxa.common.base.Result;
import com.gxa.domain.entity.ProductInfo;
import com.gxa.service.ProductInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
@Tag(name = "上货", description = "上货信息管理相关接口")
@CrossOrigin(origins = "*")
public class ProductInfoController {
    @Autowired
    private ProductInfoService productInfoService;

    @GetMapping("/barcode/{barcodeNo}")
    public Result<ProductInfo> getProductByBarcode(@PathVariable String barcodeNo) {
        ProductInfo product = productInfoService.getByBarcodeNo(barcodeNo);
        System.out.println("Controller 查询到的 product: " + product);
        try {
            com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
            String json = mapper.writeValueAsString(product);
            System.out.println("ProductInfo 手动序列化为 JSON: " + json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Result<ProductInfo> result = Result.buildSuccess(product);
        System.out.println("Controller 返回的 Result: " + result);
        return result;
    }

    @PostMapping("/stock/update")
    public Result<Void> updateStock(@RequestBody List<Map<String, Object>> updateList) {
        for (Map<String, Object> item : updateList) {
            String barcodeNo = (String) item.get("barcode");
            Integer quantity = (Integer) item.get("quantity");
            productInfoService.updateStock(barcodeNo, quantity);
        }
        return Result.buildSuccess();
    }

    @GetMapping("/list")
    public List<ProductInfo> list() {
        return productInfoService.list();
    }
} 